// 导入mockjs
const Mock = require("mockjs");
Mock.mock("/api/login", "get", {
  code: 200,
  data: {
    userinfo: {
      username: "@cname", // 返回随机中文名
      "sex|1": [0, 1],
      avatar: "@image('200x200')",
    },
    token: "@word(50)", // 返回长度为50的英文字符串
  },
});
Mock.mock("/api/dirc", "get", {
  code: 200,
  counts: "@integer(3000, 8000)",
  "data|5": [
    {
      value: [
        "@integer(20, 100)",
        "@integer(20, 100)",
        "@integer(20, 100)",
        "@integer(20, 100)",
        "@integer(20, 100)",
        "@integer(20, 100)",
      ],
      name: "张三",
      areaStyle: {
        normal: {
          color: "@hex()", // 选择区域颜色
        },
      },
    },
  ],
});
Mock.mock("/api/obj", "get", {
  code: 200,
  counts: "@integer(3000, 8000)",
  data: [
    { value: "@integer(20, 100)", name: "前端" },
    { value: "@integer(20, 100)", name: "UI" },
    { value: "@integer(20, 100)", name: "Python" },
    { value: "@integer(20, 100)", name: "c++" },
    { value: "@integer(20, 100)", name: "Java" },
  ],
});
Mock.mock("/api/new", "get", {
  code: 200,
  counts: "@integer(3000, 8000)",
  "data|3": [
    [
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
    ],
  ],
});

Mock.mock("/api/paiming", "get", {
  code: 200,
  counts: "@integer(3000, 8000)",
  "data|2": [
    [
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
      "@integer(20, 100)",
    ],
  ],
});

Mock.mock("/api/pai", "get", {
  code: 200,
  counts: "@integer(3000, 8000)",
  "data|4": [
    {
      name: "java试题",
      counts: "22212",
    },
    {
      name: "前端试题",
      counts: "12121",
    },
  ],
});
Mock.mock("/api/hot", "get", {
  code: 200,
  // counts: "@integer(3000, 8000)",
  "data|8": [
    {
      name: "关键词-@increment",
      counts: "@integer(50, 400)",
      inc: "@integer(20, 100)%",
    },
  ],
});

Mock.mock("/api/xiao", "get", {
  code: 200,
  data: [
    { value: "@integer(60, 400)", name: "Baidu" },
    { value: "@integer(60, 400)", name: "Direct" },
    { value: "@integer(60, 400)", name: "Email" },
    { value: "@integer(60, 400)", name: "Google" },
    { value: "@integer(60, 400)", name: "Union Ads" },
    { value: "@integer(60, 400)", name: "Bing" },
    { value: "@integer(60, 400)", name: "Video Ads" },
    { value: "@integer(60, 400)", name: "Others" },
  ],
});

const count = 101;
const originList = [];
for (let i = 0; i < count; i++) {
  originList.push(
    Mock.mock({
      index: "@increment",
      bianhao: "@word(1)@id",
      tixing: "@pick(['简答','单选','多选'])",
      bianhao2: "@word(1)@id@word(1)@id",
      time: "@datetime",
      huida: "@integer(200, 1000)s",
      zhengque: "@integer(0, 100)%",
      people: "@cname",
    })
  );
}

Mock.mock("/api/tizu", "post", (params) => {
  const body = JSON.parse(params.body);
  let { page, pageSize, keyword } = body;
  const arr = [];
  originList.forEach((item) => {
    // console.log(item, "标志", keyword);
    if (item.bianhao.includes(keyword)) {
      arr.push(item);
    }
  });
  let total = arr.length;
  let len = total / pageSize;
  const totalPage = Number.isInteger(len) ? len + 1 : len;
  const list = arr.slice((page - 1) * pageSize, page * pageSize);
  return {
    code: 200,
    message: "success",
    data: {
      page,
      pageSize,
      total,
      totalPage,
      list,
    },
  };
});
Mock.mock("/api/del", "post", (body) => {
  const id = body.body;
  // const body = JSON.parse(params.body);
  // console.log(id, 2536);
  const index = originList.findIndex((item) => item.index === id);
  originList.splice(index, 1);
  return {
    code: 200,
    message: "删除成功",
  };
});
